package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ActCategory;
import com.ruoyi.system.domain.ActTitle;
import com.ruoyi.system.domain.Game;
import com.ruoyi.system.dto.ResultDto;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IActCategoryService;
import com.ruoyi.system.vo.ActPrizeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.ActPrize;
import com.ruoyi.system.service.IActPrizeService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 获奖名单Service业务层处理
 *
 * @author xuyk
 * @date 2022-06-22
 */
@Service
public class ActPrizeServiceImpl implements IActPrizeService
{
    @Autowired
    private ActPrizeMapper actPrizeMapper;

    @Autowired
    private ActCategoryMapper actCategoryMapper;

    @Autowired
    private GameMapper gameMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private ActTitleMapper actTitleMapper;

    @Autowired
    private IActCategoryService actCategoryService;

    /**
     * 查询获奖名单
     *
     * @param id 获奖名单主键
     * @return 获奖名单
     */
    @Override
    public ActPrize selectActPrizeById(Long id)
    {
        return actPrizeMapper.selectActPrizeById(id);
    }

    /**
     * 查询获奖名单列表
     *
     * @param actPrize 获奖名单
     * @return 获奖名单
     */
    @Override
    public List<ActPrize> selectActPrizeList(ActPrize actPrize)
    {
        return actPrizeMapper.selectActPrizeList(actPrize);
    }

    /**
     * 新增获奖名单
     *
     * @param actPrize 获奖名单
     * @return 结果
     */
    @Override
    public int insertActPrize(ActPrize actPrize)
    {

        //获取作品类别名称
        if(actPrize.getCategoryId() != null){
            ActCategory actCategory = actCategoryMapper.selectActCategoryById(actPrize.getCategoryId());
            if(actCategory != null){
                actPrize.setCategoryName(actCategory.getName());
            }
        }

        //获取大赛名称
        if(actPrize.getGameId() != null){
            Game game = gameMapper.selectGameById(actPrize.getGameId());
            if(game != null){
                actPrize.setGameName(game.getName());
            }
        }

        //获取命名标题
        if(actPrize.getTitleId() != null){
            ActTitle actTitle = actTitleMapper.selectActTitleById(actPrize.getTitleId());
            if(actTitle != null){
                actPrize.setTitle(actTitle.getTitle());
            }
        }

        actPrize.setCreateTime(DateUtils.getNowDate());
        return actPrizeMapper.insertActPrize(actPrize);
    }

    /**
     * 修改获奖名单
     *
     * @param actPrize 获奖名单
     * @return 结果
     */
    @Override
    public int updateActPrize(ActPrize actPrize)
    {
        actPrize.setUpdateTime(DateUtils.getNowDate());
        return actPrizeMapper.updateActPrize(actPrize);
    }

    /**
     * 批量删除获奖名单
     *
     * @param ids 需要删除的获奖名单主键
     * @return 结果
     */
    @Override
    public int deleteActPrizeByIds(String ids)
    {
        return actPrizeMapper.deleteActPrizeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除获奖名单信息
     *
     * @param id 获奖名单主键
     * @return 结果
     */
    @Override
    public int deleteActPrizeById(Long id)
    {
        return actPrizeMapper.deleteActPrizeById(id);
    }


    /**
     * 通过gameId和actNo查询  actNo是否存在
     * @param actNo
     * @param gameId
     * @return
     */
    @Override
    public boolean isExistActNo(String actNo,Long gameId,Long actId){
        List<ActPrize> list = actPrizeMapper.selectByGameIdAndActNo(gameId,actNo);
        if(list != null && list.size()>0){
            if(list.size()>1){
                return true;
            }else {
                if(list.get(0).getId().equals(actId)){
                    return false;
                }else{
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 导入获奖作品数据
     * @param actPrizeVOS
     * @param gameId
     * @param curUser 当前登录用户
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultDto importActPrizeData(List<ActPrizeVO> actPrizeVOS, Long gameId, SysUser curUser){
        ResultDto resultDto = new ResultDto();
        //校验数据
        if(gameId == null || gameId <= 0){
            resultDto.setMessage("请选择大赛名称");
            return  resultDto;
        }
        if(actPrizeVOS == null || actPrizeVOS.size() == 0){
            resultDto.setMessage("导入的数据为空");
            return resultDto;
        }

        Game game = gameMapper.selectGameById(gameId);
        if(game == null){
            resultDto.setMessage("请选择大赛名称");
            return  resultDto;
        }

        //查询有叶子节点的父节点集合
        List<ActCategory> parentTreeNodes = actCategoryService.findTreeParentNodes();
        String parentCategoryNames = "";
        if(parentTreeNodes != null && parentTreeNodes.size() > 0){
            for(ActCategory category : parentTreeNodes){
                parentCategoryNames += category.getName() + ",";
            }
        }

        SysDept curDept = sysDeptMapper.selectDeptById(curUser.getDeptId());
        SysDept curSchool = null;

        List<String> actNos = new ArrayList<>();
        for(int i=0; i<actPrizeVOS.size();i++){
            //Act act = new Act();

            int row = i + 2;
            String prefixMsg = "第" + row + "行的";
            ActPrizeVO actPrizeVO = actPrizeVOS.get(i);
            actPrizeVO.setGameId(gameId);

            if(actNos.contains(actPrizeVO.getActNo())){
                resultDto.setMessage("导入的表格中【参赛编号】"+ actPrizeVO.getActNo() +" 存在重复数据！" );
                return  resultDto;
            }else{
                actNos.add(actPrizeVO.getActNo());
            }


            if(StringUtils.isBlank(actPrizeVO.getPrizeLevel())){
                resultDto.setMessage(prefixMsg +"【获奖等级】为空！" );
                return  resultDto;
            }
//
//            //作品类别不能为父级节点
//            if(StringUtils.isNotBlank(parentCategoryNames) && parentCategoryNames.contains(actPrizeVO.getCategoryName() + ",")){
//                resultDto.setMessage(prefixMsg +"【作品类别】名称错误！" );
//                return  resultDto;
//            }
//
//            if(StringUtils.isBlank(actPrizeVO.getTitle())){
//                resultDto.setMessage(prefixMsg + "【命题名称】为空！");
//                return  resultDto;
//            }
//
//            if(StringUtils.isBlank(actPrizeVO.getActNo())){
//                resultDto.setMessage(prefixMsg + "【参赛编号】为空！");
//                return  resultDto;
//            }
//
//            if(StringUtils.isBlank(actPrizeVO.getActName())){
//                resultDto.setMessage(prefixMsg + "【作品名称】为空！");
//                return  resultDto;
//            }
//
//            if(StringUtils.isBlank(actPrizeVO.getAuthor())){
//                resultDto.setMessage(prefixMsg + "【作者】为空！");
//                return  resultDto;
//            }
//
//            if(StringUtils.isBlank(actPrizeVO.getAuthorPhone())){
//                resultDto.setMessage(prefixMsg + "【作者电话】为空！");
//                return  resultDto;
//            }

            //校验是否存在相同参数编号的作品
            if(isExistActNo(actPrizeVO.getActNo(), gameId,null)){
                resultDto.setMessage(prefixMsg + "【参赛编号】"+ actPrizeVO.getActNo() +"已存在！");
                return  resultDto;
            }

            // 查看类别是否存在
            List<ActCategory> actCategories = actCategoryMapper.selectListByCategoryName(actPrizeVO.getCategoryName());
            if(actCategories == null || actCategories.size() == 0){
//                resultDto.setMessage(prefixMsg + "【作品类别】不存在！");
//                return  resultDto;
                actPrizeVO.setCategoryId(null);
                actPrizeVO.setCategoryName(actPrizeVO.getCategoryName());
            }else{
                actPrizeVO.setCategoryId(actCategories.get(0).getId());
                actPrizeVO.setCategoryName(actCategories.get(0).getName());
            }

            //查看命题名称是否存在
            if(StringUtils.isNotBlank(actPrizeVO.getTitle())){
                List<ActTitle> titles = actTitleMapper.selectByGameIdAndTitleAndCategoryId2(actPrizeVO);
                if(titles == null || titles.size() == 0){
                    actPrizeVO.setTitle(actPrizeVO.getTitle());
                    actPrizeVO.setTitleId(null);
                }else{
                    actPrizeVO.setTitle(titles.get(0).getTitle());
                    actPrizeVO.setTitleId(titles.get(0).getId());
                }
            }

            if(StringUtils.isNotBlank(actPrizeVO.getSchool())){
                actPrizeVO.setSchool(actPrizeVO.getSchool().trim());
            }
            if(StringUtils.isNotBlank(actPrizeVO.getFaculty())){
                actPrizeVO.setFaculty(actPrizeVO.getFaculty().trim());
            }

//            if(StringUtils.isBlank(actPrizeVO.getSchool()) || actPrizeVO.getSchool().length() < 4){
//                resultDto.setMessage(prefixMsg +"【学校】名称不对！" );
//                return  resultDto;
//            }
//            if(StringUtils.isBlank(actPrizeVO.getFaculty())){
//                resultDto.setMessage(prefixMsg +"【院系】为空！" );
//                return  resultDto;
//            }
            //查询学校是否存在
            actPrizeVO.setSchoolId(null);
            actPrizeVO.setSchool(actPrizeVO.getSchool());

            //查询学校下的院系是否存在
            actPrizeVO.setFacultyId(null);
            actPrizeVO.setFaculty(actPrizeVO.getFaculty());

        }

        for(int i=0; i<actPrizeVOS.size();i++) {
            ActPrizeVO actPrizeVO = actPrizeVOS.get(i);
            ActPrize actPrize = new ActPrize();
            actPrize.setCreateBy(curUser.getLoginName());
            actPrize.setGameId(game.getId());
            actPrize.setGameName(game.getName());
            actPrize.setActNo(actPrizeVO.getActNo());
            actPrize.setActName(actPrizeVO.getActName());
            actPrize.setAuthor(actPrizeVO.getAuthor());
            actPrize.setAuthorPhone(actPrizeVO.getAuthorPhone());
            actPrize.setTeacher(actPrizeVO.getTeacher());
            actPrize.setTeacherPhone(actPrizeVO.getTeacherPhone());
            actPrize.setRemark(actPrizeVO.getRemark());
            actPrize.setMajor(actPrizeVO.getMajor());
            actPrize.setCreateTime(DateUtils.getNowDate());
            actPrize.setSchoolId(actPrizeVO.getSchoolId());
            actPrize.setSchool(actPrizeVO.getSchool());
            actPrize.setFacultyId(actPrizeVO.getFacultyId());
            actPrize.setFaculty(actPrizeVO.getFaculty());
            actPrize.setCategoryId(actPrizeVO.getCategoryId());
            actPrize.setCategoryName(actPrizeVO.getCategoryName());
            actPrize.setTitle(actPrizeVO.getTitle());
            actPrize.setTitleId(actPrizeVO.getTitleId());
            actPrize.setPrizeLevel(actPrizeVO.getPrizeLevel());
            actPrize.setSendAudit(actPrizeVO.getSendAudit());
            actPrizeMapper.insertActPrize(actPrize);
        }

        resultDto.setSuccess(true);
        return resultDto;
    }
}
